﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Ext.Net;

public partial class SYS_POPEDOM_MyMenu_Edit : BasePage
{
    private static DataTable tbMyMenu = new DataTable();

    private static DataTable tbMyMenu2 = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRoleMenu();
            tbMyMenu = GetMyMenu();
            BindDataMyMenu();
            BindRoleMenu();
        }
    }

    //绑定角色功能
    private void BindRoleMenu()
    {
        DataTable tmpDB = CommonLib.BLL.SYS_SHORTCUT.GetRoleMenuList(GetSeledRoles(), "MENU_NO").Tables[0];
        this.Store1.DataSource = tmpDB;
        this.Store1.DataBind();
    }

    //获得选择角色
    private string GetSeledRoles()
    {
        string result = "";

        DataTable tmpDB = CommonLib.BLL.SYS_SHORTCUT.GetUserRoleList(BS_LoginUserID, "ROLE_ID").Tables[0];
        for (int i = 0; i < tmpDB.Rows.Count; i++)
        {
            result = result + "'" + tmpDB.Rows[i]["ROLE_ID"] + "',";
        }

        if (result.Length > 0)
        {
            result = result.Substring(0, result.Length - 1);
        }


        if (result.Length == 0)
            result = "''";
        return result;
    }

    //获得我的桌面菜单数据表

    private DataTable GetMyMenu()
    {
        DataTable tmpDB = CommonLib.BLL.SYS_SHORTCUT.GetList(" USER_ID ='" + BS_LoginUserID + "'", "USER_ID").Tables[0];
        return tmpDB;
    }

    //绑定我的菜单数据
    private void BindDataMyMenu()
    {
        this.Store2.DataSource = tbMyMenu;
        this.Store2.DataBind();
    }

    //增加选择菜单到当前数据表
    [DirectMethod]
    public void AddSeledRowsToTB(string menuNo, string menuName)
    {

        bool existed = false;
        for (int i = 0; i < tbMyMenu.Rows.Count; i++)
        {
            if (menuNo.Trim() == tbMyMenu.Rows[i]["MENU_NO"].ToString().Trim())
                existed = true;
        }

        if (!existed)
        {
            DataRow dr = tbMyMenu.NewRow();
            dr["USER_ID"] = BS_LoginUserID;
            dr["MENU_NO"] = menuNo.Trim();
            dr["MENU_NAME"] = menuName.Trim();
            tbMyMenu.Rows.Add(dr);
        }


        BindDataMyMenu();
    }


    [DirectMethod]
    public void DelSeledRowsFromTB(string menuNo, string menuName)
    {
        //bool existed = false;
        //for (int i = 0; i < tbMyMenu.Rows.Count; i++)
        //{
        //    if (menuNo.Trim() == tbMyMenu.Rows[i]["MENU_NO"].ToString().Trim())
        //        existed = true;
        //}

        DataRow[] drArray = tbMyMenu.Select("MENU_NO='" + menuNo + "'");
        foreach (DataRow dr in drArray)
        {
            //dr.Delete();
            tbMyMenu.Rows.Remove(dr);
        }

        BindDataMyMenu();
    }
    //保存选择菜单
    [DirectMethod]
    public void SaveMyMenu()
    {
        CommonLib.BLL.SYS_SHORTCUT.AddMyMenu(tbMyMenu);
    }


}
